﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.adapter.Popwindows_Adapter;
import com.badibadi.fragment.ClubOverview0Fragmente;
import com.badibadi.fragment.ClubOverview0Fragmente.getFragmentData_2;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Results;
import com.libjph.util.StringUtil;
import com.view.my_view.SlideMenu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

//ClubListActivity 俱乐部总览
public class ClubListActivity extends BaseActivity implements OnClickListener {//getIntent();

	public TabHost mTabHost;
	/**
	 * tabHost的点击项
	 */

	private int TabHostItem = 0;
	private ClubOverview0Fragmente fragmente0;
	private int add_in;
	private Bundle bun = null;
	private ClueTypeModel clubTypeModel;
	private TextView club_overview_title;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					club_overview_title.setText(msg.getData().getString(
							"tital_name"));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:
//				   
				club_overview_title.setText(getResources().getString(
						R.string.ClubOverView));
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.club_overview);
		init();
		InitializeFragment("new");
		initTabHost(R.string.Nearby1);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Constants.Activity_tital_handler = handler;
	}

	private void init() {
		// TODO Auto-generated method stub
		/**
		 * IntexActivity(IndexClub0Fragment)中传过来的种类，还需要做修改（该隐藏的隐藏）
		 */
		clubTypeModel = (ClueTypeModel) getIntent().getSerializableExtra(
				"ClubListActivity");
		club_overview_title = (TextView) findViewById(R.id.club_overview_title);
		if (clubTypeModel != null) {
			if (!StringUtil.isNullOrEmpty(clubTypeModel.getName())) {
				((TextView) (this.findViewById(R.id.club_overview_title)))
						.setText("" + clubTypeModel.getName());
			}
		} else {
			((TextView) (this.findViewById(R.id.club_overview_title)))
					.setText(getResources().getString(R.string.l_xb51));
		}
		this.findViewById(R.id.club_overview_back).setOnClickListener(this);
		this.findViewById(R.id.add_Club).setOnClickListener(this);
	}

	protected void initTabHost(int stringTab3) {
		// TODO Auto-generated method stub

		mTabHost = (TabHost) findViewById(R.id.club_list_tabhost);
		mTabHost.setup();
		mTabHost.addTab(mTabHost
				.newTabSpec("club_list one")
				.setIndicator(getResources().getString(R.string.Newest),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		mTabHost.addTab(mTabHost
				.newTabSpec("club_list two")
				.setIndicator(getResources().getString(R.string.Hottest),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		mTabHost.addTab(mTabHost
				.newTabSpec("club_list three")
				.setIndicator(getResources().getString(stringTab3),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		TabHostUtils.SetTabHost(this, mTabHost);
		mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				// Toast.makeText(getApplicationContext(),
				// "" + mTabHost.getCurrentTab(), 0).show();
				switch (mTabHost.getCurrentTab()) {
				case 0:
					if (clubTypeModel == null) {
						handler.sendEmptyMessage(2);
					}
					InitializeFragment("new");
					break;
				case 1:
					if (clubTypeModel == null) {
						handler.sendEmptyMessage(2);
					}
					InitializeFragment("hot");
					break;
				case 2:
					if (clubTypeModel == null) {
						handler.sendEmptyMessage(2);
					}
					InitializeFragment("nearby");
					break;
				default:
					if (clubTypeModel == null) {
						handler.sendEmptyMessage(2);
					}
					InitializeFragment("new");
					break;
				}
			}
		});
	}

	/**
	 * 初始化fragment
	 */
	protected void InitializeFragment(String type) {
		ViewFragmentHolder viewHolder = new ViewFragmentHolder();
		fragmente0 = new ClubOverview0Fragmente();
		bun = new Bundle();
		bun.putString("type", type);
		bun.putString("uid", Utils.getUid(ClubListActivity.this));
		if (clubTypeModel != null) {
			bun.putString("TYPE_id", clubTypeModel.getId());
		}
		fragmente0.setArguments(bun);
		viewHolder.transaction.replace(R.id.club_overview_layout, fragmente0);
		viewHolder.transaction.commit();
		add_in = 0;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.club_overview_back:
			// this.finish();
			
			fragmente0.getSearchIsReturnUp_2(new getFragmentData_2() {

				@Override
				public void getIsReturnUp_2(boolean isReturn) {
					clubTypeModel = null;
					// TODO Auto-generated method stub
				}
			});
			break;
		case R.id.add_Club:
			System.out.println("util"+Utils.getUid(ClubListActivity.this));
			if (!Utils.getUid(ClubListActivity.this).equals("")) {
				Intent intent = new Intent(ClubListActivity.this,
						CreationNewClubActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.l_xb10), 0).show();
			}
			break;

		default:
			break;
		}
	}
}
